package com.jservice.web.sysAndriodType;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jservice.framework.dto.Filter;
import com.jservice.framework.dto.Pageable;
import com.jservice.framework.dto.Pager;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.framework.utils.EncryptUtils;
import com.jservice.framework.utils.StringUtils;
import com.jservice.model.SmkAndriodType;
import com.jservice.model.SysOffice;
import com.jservice.model.SysUser;
import com.jservice.model.SysUserRole;
import com.jservice.web.base.JBaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: huoo
 * @version: 1.0.0
 * @Description:
 * @Date: create in 11:03 2017/8/21
 * @Modified By:
 */
@Service("androidTypeService")
@Transactional(readOnly = true)
public class AndroidTypeService extends JBaseService {

    public static final SmkAndriodType DAO= new SmkAndriodType();

    public static final AndroidTypeService me(){return SpringContextUtil.getBean("androidTypeService");}

    @Resource
    private Sequence idWorKer;

    public Pager findPage(Pageable pageable) {
        List<Filter> filters = pageable.getFilters();
        pageable.setFilters(filters);

        StringBuffer selectSql = new StringBuffer("select  t.* ");
        StringBuffer whereSql = new StringBuffer();
        whereSql.append(" from ").append(SmkAndriodType.TABLE_NAME).append(" t ");

        LinkedList<Object> paramValue = new LinkedList<Object>();

        fillFilters(whereSql,paramValue,pageable.getFilters());
        fillOrders(whereSql,pageable.getOrders());
        Page page = Db.paginate(pageable.getPageNumber(), pageable.getPageSize(), selectSql.toString(), whereSql.toString(), paramValue.toArray());
        Pager pager = new Pager(page.getList(),page.getTotalRow(),page.getTotalPage(),pageable);
        return pager;
    }

    public boolean androidTypeExists(String androidType) {
        if(androidType == null){
            return false;
        }
        SmkAndriodType smkAndriodType = DAO.findFirst("select * from "+SmkAndriodType.TABLE_NAME+" where andriod_type = ? ",androidType);

        return smkAndriodType != null;
    }


    @Transactional
    public boolean save(SmkAndriodType smkAndriodType) {
        if(smkAndriodType == null){
            return false;
        }
        smkAndriodType.setId(idWorKer.nextIdStr());
        smkAndriodType.save();
        return true;
    }

    public Record findById(String id) {
        Record androidType = Db.findFirst("select * from "+SmkAndriodType.TABLE_NAME+" where id = ? ",id);
        return androidType;
    }

    @Transactional
    public boolean update(SmkAndriodType smkAndriodType) {
        if (smkAndriodType == null){
            return false;
        }
        smkAndriodType.update();
        return true;
    }




}
